package com.wash.shoes.controller.pc;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.entity.SysUserPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ljg
 * @since 2024-09-21
 */
@Api(tags = {"PC-员工信息表"})
@RestController
@RequestMapping({"/dev-api/sysUser", "/sysUser"})
public class SysUser2Controller {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    ShopService shopService;

    @ApiOperation("员工信息表列表")
    @PostMapping("/list")
    public R<Page<SysUserPo>> pageList(@RequestBody SysUserPo sysUserPo) {
        Page<SysUserPo> page = sysUserService.pageList(sysUserPo);
        return R.ok(page);
    }

    @ApiOperation("员工信息表列表")
    @PostMapping("/noShopList")
    public R<Page<SysUserPo>> noShopList(@RequestBody SysUserPo sysUserPo) {
        Page<SysUserPo> page = sysUserService.noShopList(sysUserPo);
        return R.ok(page);
    }

    @ApiOperation("员工绑定到门店")
    @GetMapping("/binding")
    public R<String> binding(String userId, String shopId) {

        SysUserPo sysUserPo = sysUserService.getById(userId);
        if (StrUtil.isNotEmpty(sysUserPo.getShopId())) {
            throw new WashException("该用户已经绑定门店，请先解绑");
        }
        sysUserPo.setShopId(shopId);
        sysUserService.saveOrUpdate(sysUserPo);
        return R.ok("suc");
    }

    @ApiOperation("员工解绑")
    @GetMapping("/unbinding")
    public R<String> binding(String userId) {

        SysUserPo sysUserPo = sysUserService.getById(userId);
        sysUserPo.setShopId("");
        sysUserService.saveOrUpdate(sysUserPo);
        return R.ok("suc");
    }


    @ApiOperation("员工的门店信息，返回为空就不存在门店")
    @GetMapping("/userShop")
    public R<ShopPo> userShop(String userId) {

        SysUserPo sysUserPo = sysUserService.getById(userId);
        if (sysUserPo.getShopId() == null) {
            return R.ok();
        } else {
            return R.ok(shopService.getById(sysUserPo.getShopId()));
        }
    }

}

